#include <fstream>
#include "CKMsgQueue.h"
#include "CKMsg.h"
#include "../_Module/CKEngineLog.h"
#include <iostream>



namespace K_Engine
{
	void CKMsgQueue::CacheMsgInFile()
	{
		if(m_tempfile.empty())
		{
			return;
		}
		std::ofstream ofs;
		ofs.open(m_tempfile.c_str());
		
		for(int i = 0, size = m_queue.Size() ; i < size ; i++)
		{
			ofs<<m_queue.Pop().c_str()<<std::endl;
		}
		ofs.close();
	}
	
	
	void CKMsgQueue::LoadMsgInFile()
	{
		if(m_tempfile.empty())
		{
			return;
		}
		std::ifstream ifs;
		ifs.open(m_tempfile.c_str());
		std::string buff;
		
		while(getline(ifs, buff))
		{
			if(!buff.empty() && '#' != buff[0])
			{
				m_queue.Push(buff);
				buff.clear();
			}
		}
		ifs.close();
	}
	
	
	bool CKMsgQueue::init(std::string strTempMsgFile)
	{
		ChangeMsgCacheFile(strTempMsgFile);
		LoadMsgInFile();
		
		m_right = true;
		return true;
	}
	
	
	void CKMsgQueue::ChangeMsgCacheFile(std::string tempfile)
	{
		m_tempfile = tempfile;
	}
	
	
	CKMsgQueue::CKMsgQueue() :
		m_tempfile(""),
		m_strLastError(""),
		m_distributed(NULL),
		m_right(false),
		m_run(false)
	{
	}
	
	
	CKMsgQueue::~CKMsgQueue()
	{
		if( "" != m_tempfile)
		{
			CacheMsgInFile();
		}
		else
		{
			Clear();
		}
	}
	
	
	std::string CKMsgQueue::strGetLastError()
	{
		return m_strLastError;
	}
	
	
	void CKMsgQueue::PushMsg(std::string msg)
	{
		m_queue.Push(msg);
	}
	
	
	std::string CKMsgQueue::PopMsg()
	{
		return m_queue.Pop();
	}
	
	
	void CKMsgQueue::Clear()
	{
		for(int i = 0, size = m_queue.Size() ; i < size ; i++)
		{
			m_queue.Pop();
		}
	}
	
	
	void CKMsgQueue::MessageLoop(void* SetWorkObject, setworkfunc _setworkfunc)
	{
		if(!m_right)
			return;
		if(!_setworkfunc)
			return;
		m_run = true;
		CKEngineLog log;
		while(m_run)
		{
			CKMsg temp;
			std::string s = PopMsg();
			if(!temp.ParserMSG(s))
			// if(!temp.ParserMSG(PopMsg()))
			{//消息解析出错
				std::string strtemp = "消息解析出错 {";
				strtemp += temp.GetErrorMSG() + "}";
				log.Log(strtemp,/*CKEngineLog::ERROR*/LOG_TYPE::ERROR);
			}
			else if(CKMsg::Work == temp.GetType())
			{//工作消息
				// std::string strTemp = "工作指派 {";
				// strTemp += temp.GetRuleName() + "} {";
				// strTemp += temp.GetParams() + "}";
				// log.Log(strTemp, LOG_TYPE::NORMAL);
				_setworkfunc(SetWorkObject, temp.FormatMSG());
			}
			else if(CKMsg::Return == temp.GetType())
			{//返回消息
				if(NULL != m_distributed)
				{//如果存在分布式模块
					if(!temp.GetRuleName().empty())
					{
						temp.SetType(CKMsg::Distributed);
						// distributed.doing something....
					}
				}
				else
				{//否则本地处理
					if(!temp.GetRuleName().empty())
					{
						temp.SetType(CKMsg::Work);
						PushMsg(temp.FormatMSG());
					}
				}
			}
			else if(CKMsg::Error == temp.GetType())
			{//错误消息

				std::string strtemp = "错误消息{";
				strtemp += temp.GetRuleName() + "}{" + temp.GetParams() + "}";
				log.Log(strtemp,LOG_TYPE::ERROR);
			}
		}
	}
	
	
	void CKMsgQueue::KillMQDealMsg()
	{
		m_run = false;
	}
	
	
	void CKMsgQueue::SetDistributed(CKDistributed *distributed)
	{
		m_distributed = distributed;
	}
	
	
	void CKMsgQueue::SetLastError(std::string strlasterror)
	{
		m_strLastError.clear();
		m_strLastError = strlasterror;
	}
}
